﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Common;
using Models;

namespace CSMF
{

  

    public partial class PostCommentHandler : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
           

        }

        [WebMethod]
        public static PostResult Post(string PostContent, int ObjId, int ObjTypeId, string GuidValue, string ValidateCode)
        {
            var _webWorkContext = new WebWorkContext();
            var user = _webWorkContext.CurrentUser;
            if(!user.IsRegistered())
            {
                return new PostResult()
                {
                    Status = "fail",
                    Message = "请先登录"
                };
            }

            if(string.IsNullOrEmpty(PostContent.Trim()))
            {
                return new PostResult()
                {
                    Status = "fail",
                    Message = "请填写评论内容"
                };
            }

            if (BaseService.NeedValidateCode())
            {
                bool checkSuccess = BaseService.CheckValidateCode(GuidValue, ValidateCode);
                if (checkSuccess == false)
                {
                    return new PostResult()
                               {
                                   Status = "fail",
                                   Message = "请填写正确验证码"
                               };
                }
            }

          
            var commentService = new CommentService();
            commentService.PostComment(user.Id, PostContent, ObjId, (ObjType) ObjTypeId);



            if ((ObjType)ObjTypeId == ObjType.微力十足)
            {
                var video = VideoInfoManager.GetVideoInfoByID(ObjId);
                var link = ConfigurationManager.AppSettings["AppDomain"] +
                           string.Format("MerakDetail.aspx?id={0}", ObjId);

                var content = string.Format("标题:{0} 链接: {2} 评论内容:{1}", video.Title, PostContent, link);

                var _sinaService = new SinaService();
                _sinaService.PostToWeibo(user.Id, content);
            }

            if ((ObjType)ObjTypeId == ObjType.市长带你游)
            {
                var video = MayorManager.GetMayorByID(ObjId);

                var link = ConfigurationManager.AppSettings["AppDomain"] +
                          string.Format("MayorDetail.aspx?id={0}", ObjId);

                var content = string.Format("标题:{0} 链接: {2}  评论内容:{1}", video.Title, PostContent, link);
                var _sinaService = new SinaService();
                _sinaService.PostToWeibo(user.Id, content);
            }

            if ((ObjType)ObjTypeId == ObjType.媒体中心新闻)
            {
                var video = NewsInfoManager.GetNewsInfoByID(ObjId);
                var link = ConfigurationManager.AppSettings["AppDomain"] +
                       string.Format("NewsDetail.aspx?id={0}", ObjId);
                var content = string.Format("标题:{0} 链接: {2} 评论内容:{1}", video.Title, PostContent, link);
                var _sinaService = new SinaService();
                _sinaService.PostToWeibo(user.Id, content);
            }

            if ((ObjType)ObjTypeId == ObjType.主题活动)
            {
                var video = NewsInfoManager.GetNewsInfoByID(ObjId);
                var link = ConfigurationManager.AppSettings["AppDomain"] +
                         string.Format("ThemeDetail.aspx?id={0}", ObjId);
                var content = string.Format("标题:{0} 链接: {2} 评论内容:{1}", video.Title, PostContent,link);
                var _sinaService = new SinaService();
                _sinaService.PostToWeibo(user.Id, content);
            }
            
            var postCommentModel = GetPostCommentModel(ObjId, ObjTypeId, 0);

            return new PostResult()
                       {
                           PostCommentModel = postCommentModel,
                           Status = "success"
                       };

        }

       

        [WebMethod]
        public static PostResult Paging( int ObjId, int ObjTypeId,  int CurrentPage)
        {
          
            var postCommentModel = GetPostCommentModel(ObjId, ObjTypeId, CurrentPage);


            var result = new PostResult();
            result.Status = "success";
            result.PostCommentModel = postCommentModel;
            return result;
        }

         private static PostCommentModel GetPostCommentModel(int ObjId, int ObjTypeId, int pageIndex)
        {
            var commentService = new CommentService();
            var pageSize = int.Parse(ConfigurationManager.AppSettings["CommentPageSize"]);
            var postCommentModel = new PostCommentModel();
            postCommentModel.CommentInfos = commentService.GetCommentsByObjId(ObjId,
                                                                              (ObjType) ObjTypeId, pageIndex,
                                                                              pageSize);
            var pageInfo = new PageInfo()
                               {
                                   PageIndex = postCommentModel.CommentInfos.PageIndex,
                                   PageSize = postCommentModel.CommentInfos.PageSize,
                                   TotalCount = postCommentModel.CommentInfos.TotalCount,
                                   TotalPages = postCommentModel.CommentInfos.TotalPages,
                                   HasPreviousPage = postCommentModel.CommentInfos.HasPreviousPage,
                                   HasNextPage = postCommentModel.CommentInfos.HasNextPage,
                               };

            postCommentModel.PageInfo = pageInfo;
            return postCommentModel;
        }
    }


    public class PostResult
    {
        public string Message { get; set; }
        public string Status { get; set; }
        public PostCommentModel PostCommentModel { get; set; }
    }
}